import Vue from 'vue'
import Vuex from 'vuex'
// vuex 持久化
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)
/**
 * 持久化
 */
const vuexLocal = new VuexPersistence({
  key: 'yuan',
  storage: window.sessionStorage
})
/**
 * 配置vuex
 * 使用全局缓存
 */

export default new Vuex.Store({
  state: {
    count: 0,
    serverDate: '', // 服务器时间
    token: '', // 用户token
    roles: [], // 角色信息
    localData: {}, // 电脑日志数据
    localDataStatus: false, // 电脑日志状态配置
    addRouter: [], // 封装的路由数据
    navigations: [], // 导航数据
    configSetting: {}, // 项目设置
    configSettingStatus: false, // 项目设置
    languageConfig: [], // 语言配置
    navigationId: '',
    user: '',
    userInfo: ''
  },
  getters: {
    getRoles: state => {
      return state.roles
    },
    addRouter: state => {
      return state.addRouter
    },
    navigations: state => {
      return state.navigations
    },
    userInfo: state => {
      return state.userInfo
    },
    navigationId: state => {
      return state.navigationId
    }
  },
  mutations: {
    login (state, obj) {
      state.roles = obj.role
      state.user = obj.user
      state.navigations = obj.navigations
      state.userInfo = obj.userInfo
    },
    increment (state) {
      state.count++
    },
    serverDate (state, serverDate) {
      state.serverDate = serverDate
    },
    token (state, token) {
      state.token = token
    },
    navigationId (state, navigationId) {
      state.navigationId = navigationId
    },
    roles (state, roles) {
      state.roles = roles
    },
    delState (state) {
      state.roles = []
      state.user = {}
      state.token = ''
      state.navigations = []
      state.navigationId = ''
      state.addRouter = []
      state.userInfo = ''
      sessionStorage.clear()
    },
    localData (state) {
      state.localDataStatus = true
      let SystemData = {}
      try {
        SystemData['code'] = '20001'
        SystemData['msg'] = 'ok'
        let data = getSystemData()
        SystemData['data'] = data
        console.log(SystemData)
      } catch (e) {
        SystemData['code'] = '40001'
        SystemData['msg'] = e
        SystemData['data'] = {}
      }
    },
    setRouters (state, obj) {
      state.addRouter = obj
    }
  },
  actions: {
    generateRoutes ({ commit }, routersData) {
      let addRouterList = []
      let mainChild = routersData[0].child
      for (let router of routersData) {
        let routerJson = {}
        routerJson['path'] = router.navigationPath
        routerJson['name'] = router.navigationName
        routerJson['component'] = loadView(router.navigationPath)
        routerJson['meta'] = {
          'title': router.navigationTitle
        }
        routerJson['children'] = []
        addRouterList = addRouterList.concat(routerJson)
      }
      for (let na of mainChild) {
        addRouterList[0].children = addRouterList[0].children.concat(filterRouters(na.child))
      }
      let router404 = {
        'path': '*',
        'name': '404',
        'redirect': '/errer/404',
        'component': (resolve) => require([`@/components/intermediate/errer/404`], resolve),
        'hidden': true,
        'meta': {
          'title': '404'
        }

      }
      addRouterList = addRouterList.concat(router404)
      commit('setRouters', addRouterList)
    }
  },
  plugins: [vuexLocal.plugin]

})

function filterRouters (routers) {
  let access = []
  routers.forEach(router => {
    let routerJson = {}
    routerJson['path'] = router.navigationPath
    routerJson['name'] = router.navigationName
    routerJson['component'] = loadView(router.navigationPath)
    routerJson['meta'] = {
      'title': router.navigationTitle
    }
    if (router.child && router.child.length > 0) {
      routerJson['children'] = filterRouters(router.child)
    }
    access.push(routerJson)
  })
  return access
}

function loadView (view) {
  return (resolve) => require([`@/components/intermediate${view}`], resolve)
}

/**
 * 电脑信息封装
 */
function getSystemData () {
  let data = {}
  let navigator = window.navigator
  let userAgent = navigator.userAgent
  let winIndex = userAgent.indexOf('Windows')
  if (winIndex > -1) {
    let obj = userAgent.substring(winIndex, userAgent.indexOf(')'))
    let arr = obj.split(';')
    if (arr.length > 0) {
      let objArr = arr[0].split('NT')
      data['system'] = 'Windows' + ' ' + objArr[1].trim().split('.')[0]
    }
  }
  let array = userAgent.split(') ')
  if (array.length === 3) {
    let arr = array[2].split(' ')
    data['browser'] = arr[0]
  }
  data['cookie'] = navigator.cookieEnabled
  data['hardwareConcurrency'] = navigator.hardwareConcurrency
  data['language'] = navigator.languages[1]
  return data
}
